*Labor Force Survey*
clear
use "Data\Core_datasets\aku_ras.dta"

sort pnr aar

merge m:1 pnr aar using "Data\dataset_with_singles.dta", keepusing(final_educ)

drop if _merge==2
drop if missing(final_educ)
drop _merge

merge m:1 pnr aar using "Data\dataset_with_types.dta" , keepusing(final_educ_field_level)
drop if _merge==2
drop _merge

drop if hwactual==99
gen nonmissing = !missing(hwusual)
sort final_educ pnr aar 
by final_educ: egen final_educ_obs=total(nonmissing)

sort final_educ_field_level pnr aar 
by final_educ_field_level: egen final_educ_field_level_obs=total(nonmissing)

sort pnr aar


**Create clustering variables

gen temp=final_educ if final_educ_obs>=50 & !missing(final_educ)

levelsof temp, matrow(programs_final_educ_50)

mat dir /*dimensions are  programs_final_educ_50[814,1]*/

drop temp

gen temp=final_educ if final_educ_obs>=100 & !missing(final_educ)

levelsof temp, matrow(programs_final_educ_100)

mat dir /*dimensions are  programs_final_educ_100[612,1]*/

drop temp

gen temp=final_educ_field_level if final_educ_field_level_obs>=50 & !missing(final_educ_field_level)

destring temp, replace

levelsof temp, matrow(programs_final_educ_field_level)

mat dir /*dimensions are  programs_final_educ_field_level[47,1]*/

drop temp

destring final_educ_field_level, replace



*average hours worked (using hwusual)
gen avr_hours_50=.

forvalues i=1(1)814{
qui sum hwusual [iweight=yfactor] if final_educ==programs_final_educ_50[`i',1]
replace avr_hours_50=r(mean) if final_educ==programs_final_educ_50[`i',1]
}

gen avr_hours_100=.

forvalues i=1(1)612{
qui sum hwusual [iweight=yfactor] if final_educ==programs_final_educ_100[`i',1]
replace avr_hours_100=r(mean) if final_educ==programs_final_educ_100[`i',1]
}

gen avr_hours_field_level=.

forvalues i=1(1)47{
qui sum hwusual [iweight=yfactor] if final_educ_field_level==programs_final_educ_field_level[`i',1]
replace avr_hours_field_level=r(mean) if final_educ_field_level==programs_final_educ_field_level[`i',1]
}


*super full time

gen evening=0 if shiftwk==3
replace evening=1 if (evenwk==1 | evenwk==2) & shiftwk==3

gen night=0 if shiftwk==3
replace night=1 if (nightwk==1 | nightwk==2) & shiftwk==3

gen saturday=0 if shiftwk==3
replace saturday=1 if (satwk==1 | satwk==2) & shiftwk==3

gen sunday=0 if shiftwk==3
replace sunday=1 if (sunwk==1 | sunwk==2) & shiftwk==3

gen home=0
replace home=1 if (homewk==1 | homewk==2)

gen overtime=0
replace overtime=1 if hourreas==16

gen sft=0 if !missing(hwusual)
replace sft=1 if !missing(hwusual) & ((hwusual>=38) | ((hwusual==37) & (evening==1 | home==1 | saturday==1 | sunday==1 | night==1 | overtime==1)))

gen irreg = 0 if !missing(hwusual) 
replace irreg = 1 if (evening==1 | home==1 | saturday==1 | sunday==1 | night==1 | overtime==1)

gen sft_50=.

forvalues i=1(1)814{
qui sum sft [iweight=yfactor] if final_educ==programs_final_educ_50[`i',1]
replace sft_50=r(mean) if final_educ==programs_final_educ_50[`i',1]
}

gen sft_100=.

forvalues i=1(1)612{
qui sum sft [iweight=yfactor] if final_educ==programs_final_educ_100[`i',1]
replace sft_100=r(mean) if final_educ==programs_final_educ_100[`i',1]
}

gen sft_field_level=.

forvalues i=1(1)47{
qui sum sft [iweight=yfactor] if final_educ_field_level==programs_final_educ_field_level[`i',1]
replace sft_field_level=r(mean) if final_educ_field_level==programs_final_educ_field_level[`i',1]
}


gen irreg_50=.

forvalues i=1(1)814{
qui sum irreg [iweight=yfactor] if final_educ==programs_final_educ_50[`i',1]
replace irreg_50=r(mean) if final_educ==programs_final_educ_50[`i',1]
}

gen irreg_100=.

forvalues i=1(1)612{
qui sum irreg [iweight=yfactor] if final_educ==programs_final_educ_100[`i',1]
replace irreg_100=r(mean) if final_educ==programs_final_educ_100[`i',1]
}

gen irreg_field_level=.

forvalues i=1(1)47{
qui sum irreg [iweight=yfactor] if final_educ_field_level==programs_final_educ_field_level[`i',1]
replace irreg_field_level=r(mean) if final_educ_field_level==programs_final_educ_field_level[`i',1]
}


*short hours

gen short_hours=0 if !missing(hwusual)
replace short_hours=1 if !missing(hwusual) & hwusual>=0 & hwusual<=36

gen short_hours_50=.

forvalues i=1(1)814{
qui sum short_hours [iweight=yfactor] if final_educ==programs_final_educ_50[`i',1]
replace short_hours_50=r(mean) if final_educ==programs_final_educ_50[`i',1]
}

gen short_hours_100=.

forvalues i=1(1)612{
qui sum short_hours [iweight=yfactor] if final_educ==programs_final_educ_100[`i',1]
replace short_hours_100=r(mean) if final_educ==programs_final_educ_100[`i',1]
}

gen short_hours_field_level=.

forvalues i=1(1)47{
qui sum short_hours [iweight=yfactor] if final_educ_field_level==programs_final_educ_field_level[`i',1]
replace short_hours_field_level=r(mean) if final_educ_field_level==programs_final_educ_field_level[`i',1]
}


******
* save files for export:

sort final_educ pnr aar 

by final_educ: gen evalp = _n==1

sort final_educ_field_level pnr aar 

by final_educ_field_level: gen evalf = _n==1

preserve
keep if evalp==1
keep final_educ avr_hours_50 short_hours_50 sft_50 irreg_50 avr_hours_100 short_hours_100 sft_100 irreg_100
drop if final_educ == .

save "Results\fig_A1\final_educ_hours_moments_corrected.dta", replace
restore

